package com.pinda.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;
import com.pinda.feign.PdTaskPickupDispatchFeign;
import com.pinda.feign.SavePdTransportOrderFeign;
import com.pinda.feign.PdOrderFeignClient;
import com.pinda.feign.PdTaskPickupDispatchFeign;
import com.pinda.service.IPdTaskPickupDispatchService;
import com.pinda.work.entity.PdTaskPickupDispatch;
import com.pinda.work.entity.PdTickupAndOrder;
import com.pinda.work.vo.Dispatch_Order;
import com.pinda.work.vo.PdTaskPickupDispatchView;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 取件、派件任务信息表 前端控制器
 * </p>
 *
 * @author lc
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-task-pickup-dispatch")
public class PdTaskPickupDispatchController {

    @Autowired
    private IPdTaskPickupDispatchService iPdTaskPickupDispatchService;

    @Autowired
    private PdTaskPickupDispatchFeign pdTaskPickupDispatchFeign;

    @Autowired
    private PdOrderFeignClient pdOrderFeignClient;

    @Autowired
    private SavePdTransportOrderFeign savePdTransportOrderFeign;


    /**
     * 派件任务
     * 待交接
     * 收件任务 1（待执行）
     */
    @GetMapping("findDispatchByTaskType")
    public Result<List<Dispatch_Order>> findDispatchByTaskType(String courierId) {
        System.out.println(courierId);
        List<Dispatch_Order> dispatchByTaskType = iPdTaskPickupDispatchService.findDispatchByTaskType(courierId);
        return Result.<List<Dispatch_Order>>builder().data(dispatchByTaskType).code(StatusCode.OK).flag(true).message("查询成功").build();
    }

    /**
     * 待妥投
     * @param courierId
     * @return
     */
    @GetMapping("findDispatchByTaskType3")
    public Result<List<Dispatch_Order>> findDispatchByTaskType3(String courierId) {
        List<Dispatch_Order> dispatchByTaskType = iPdTaskPickupDispatchService.findDispatchByTaskType3(courierId);
        return Result.<List<Dispatch_Order>>builder().data(dispatchByTaskType).code(StatusCode.OK).flag(true).message("查询成功").build();
    }
//
//    /**
//     * 查询待取件
//     * @param courierid
//     * @return
//     */
    @GetMapping ("selectreceivingByid")
    public Result findDispatch(String courierid) {
        List<Dispatch_Order> selectreceiving = iPdTaskPickupDispatchService.selectreceiving(courierid);
        System.out.println(selectreceiving);
        return Result.builder().data(selectreceiving).code(StatusCode.OK).flag(true).message("查询成功").build();
    }
//
//    /**
//     * 查询须交件
//     * @param courierid
//     * @return
//     */
    @GetMapping ("selectreceivingByid2")
    public Result findDispatch2(String courierid) {
        List<Dispatch_Order> dispatch_orders = iPdTaskPickupDispatchService.selectreceiving2(courierid);
        return Result.builder().data(dispatch_orders).code(StatusCode.OK).flag(true).message("查询成功").build();
    }
//
//    /**
//     * 查询已取消
//     * @param courierid
//     * @return
//     */
    @GetMapping ("findcanceled")
    public Result findcanceled(String courierid) {
        List<Dispatch_Order> canceled = iPdTaskPickupDispatchService.canceled(courierid);
        return Result.builder().data(canceled).code(StatusCode.OK).flag(true).message("查询成功").build();
    }

    /**
     * 拿到件之后状态修改为3（待确认）
     * 修改实际开始时间
     */
    @GetMapping("updatareceivingByid")
    public Boolean updatareceiving(String id,String orderId) {
        System.out.println(id);
        System.out.println(orderId);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.setSql("status=3,actual_start_time=now()");

        iPdTaskPickupDispatchService.update(updateWrapper);
        return pdOrderFeignClient.updateStatusById8(orderId);
    }

    /**
     * 修改状态为3（收件）
     * @param id
     * @return
     */
    @GetMapping("updatareceivingByid3/{id}")
    public Boolean updatareceivingByid3(@PathVariable("id") String id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.setSql("status=3,actual_start_time=now()");
        return iPdTaskPickupDispatchService.update(updateWrapper);
    }


    /**
     * 完成任务改状态为4（派件）
     * 修改实际完成时间
     */
    @GetMapping ("updatareceivingByid4")
    public Boolean updatareceiving4(String id,String id1,int paymentMethod) {
        return iPdTaskPickupDispatchService.updatareceivingByid4(id, id1, paymentMethod);

    }

    /**
     * 完成任务改状态为4并调用oms pdorder的修改订单状态为网点入库3（收件） 添加运单
     * @param id
     * @return
     */
    @GetMapping("updatareceivingByid5")
    public Boolean updatareceiving5 (String id,String orgId,String orderId){
        UpdateWrapper UpdateWrapper = new UpdateWrapper();
        UpdateWrapper.eq("id", id);
        UpdateWrapper.setSql("status=4,actual_end_time=now()");

        boolean b = iPdTaskPickupDispatchService.update(UpdateWrapper); // 修改任务为4
        if (b){
            Boolean savePdTransportOrder = savePdTransportOrderFeign.savePdTransportOrder(orderId);//添加新的运单
            if (b && savePdTransportOrder){
                return pdOrderFeignClient.updateStatusById3(orderId,orgId);//订单状态修改为网点入库3
            }else {
                return false;
            }
        }else {
            return false;
        }




    }

    /**
     * 查询当前快递员的已完成任务或未完成任务
     */
    @PostMapping("findDispatchByCourierId")
    public Result<List<Dispatch_Order>> findDispatchByCourierId(@RequestBody PdTaskPickupDispatch pdTaskPickupDispatch){
        System.out.println(pdTaskPickupDispatch);
        List<Dispatch_Order> dispatchByCourierId = iPdTaskPickupDispatchService.findDispatchByCourierId(pdTaskPickupDispatch);
        return Result.<List<Dispatch_Order>>builder().data(dispatchByCourierId).message("查询成功").code(StatusCode.OK).build();
    }

    /**
     * 查询取件和派件
     * @param val
     * @return
     */
    @GetMapping("/lcqueryPdTaskPickupDispatchView/{id}")
   public Result<List<PdTaskPickupDispatchView>> queryPdTaskPickupDispatchViewallController(@PathVariable("id") String val){
        return new Result<>(true,StatusCode.OK,"查询成功",this.pdTaskPickupDispatchFeign.queryPdTaskPickupDispatchViewallController(val));
    }

    @PostMapping("savePdTaskPickupDispatch")
    public boolean savePdTaskPickupDispatch(@RequestBody PdTaskPickupDispatch pdTaskPickupDispatch){

        return this.iPdTaskPickupDispatchService.save(pdTaskPickupDispatch);
    }
//    @PostMapping("findDispatchByCourierId")
//    public Result<List<Dispatch_Order>> findDispatchByCourierId (@RequestBody PdTaskPickupDispatch pdTaskPickupDispatch){
//        System.out.println(pdTaskPickupDispatch);
//        List<Dispatch_Order> dispatchByCourierId = iPdTaskPickupDispatchService.findDispatchByCourierId(pdTaskPickupDispatch);
//        return Result.<List<Dispatch_Order>>builder().data(dispatchByCourierId).message("查询成功").code(StatusCode.OK).build();
//    }
//
//


    @PostMapping("/updatecourierid")
    public void updatecourierid(@RequestBody PdTickupAndOrder pdTickupAndOrder){
        System.out.println(pdTickupAndOrder.getAgencyId());
        iPdTaskPickupDispatchService.updatecourierid(pdTickupAndOrder);

    }
    /**
     * 派发任务给快递员（派）
     * @param
     * @return
     */
    @PostMapping("/updatecourieridone")
    public void updatecourieridone(@RequestBody PdTickupAndOrder pdTickupAndOrder){
        iPdTaskPickupDispatchService.updatecourieridone(pdTickupAndOrder);

    }
}
